/*
 * @Author: 吕佳伟
 * @Date: 2021-04-08 17:07:34
 * @LastEditors: 吕佳伟
 * @LastEditTime: 2021-07-03 21:36:48
 * @Description: file content
 */
import 'dart:convert';
import 'dart:io';

import 'package:dio/dio.dart';
import 'package:stack_study/config/StackConfig.dart';
import 'package:stack_study/models/ClassResponseData.dart';

import '../models/LoginFormData.dart';
import '../models/LoginResponseData.dart';

///连接类
///吕佳伟
Future<LoginResponseData> login(String useremail, String password) async {
  print("2" + useremail + password);
  Response response = await Dio()
      .post(
    "${StackConfig.ApiUrlHead}/api/pc/${StackConfig.ApiVersion}/users/login",
    data: LoginFormData(useremail, password),
  )
      .catchError((e) {
    print(e);
  });
  print("null?");
  print(response);
  if (response != null) {
    return LoginResponseData.fromJson(response.data);
  } else {
    return null;
    // return LoginResponseData.fromJson(json.decode(
    //     """{"status":"success","token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjljNjljYjQwLTgyNDctMTFlYi1iY2M4LTc1MTQyNGUyNTI5ZCIsImlhdCI6MTYxNjgyMDk5MywiZXhwIjoxNjE2ODQ5NzkzfQ.cuYsVO0eBTaZhF0vCVt0elUscg2TU-oRLOyNEI3LrDA","data":{"class_id":["6bb15bf0-832b-11eb-af69-cf8b203a7285"],"title":"学生","photo":"default.jpg","role":"student","entry_year":"2021","_id":"9c69cb40-8247-11eb-bcc8-751424e2529d","name":"张飞翔","user_id":"41812102","org_name":"陕西师范大学","email":"zhangfeixiang@snnu.edu.cn","subOrg_name":"计算机科学学院","major_name":"软件工程","resources":[],"org_id":"6048c1c7c9ffe60020d149f6"}}"""));
  }
}

Future<ClassResponseData> getTimeTableFromStudentID(
    String studentId, String token) async {
  Options options = Options(headers: {HttpHeaders.authorizationHeader: token});
  Response response = await Dio()
      .post(
          "${StackConfig.ApiUrlHead}/api/pc/${StackConfig.ApiVersion}/timetables/getTimeTableFromStudentID",
          data: {"student_id": studentId, "year": "2020-2021", "semester": 2},
          options: options)
      .catchError((e) {
    print(e);
  });
  if (response != null) {
    return ClassResponseData.fromJson(response.data);
  } else {
    return null;
  }
}
